package com.ces.knacky.controller.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ces.knacky.framework.controller.BaseController;
import com.ces.knacky.framework.exception.DataAccessException;
import com.ces.knacky.framework.model.IEntity;
import com.ces.knacky.model.Flag;
import com.ces.knacky.model.UserTable;
import com.ces.knacky.util.ApplicationUtils;
@Controller
public class SaveRegistrationController extends BaseController {
	
	private String description;
	
	private String firstname;
	
	private String lastname;
	
	private String emailid;
	
	private Date birthdate;
	
	private String gender;
	
	private String pwd;
	
	
	@RequestMapping ("/saveRegistration.htm")	
	public String execute(){
		System.out.println("Inside Save Registration");
		UserTable user = new UserTable();
		user.setDescription(description);
		user.setFirstName(firstname);
		user.setLastName(lastname);
		user.setEmailId(emailid);
		user.setBirthDate(birthdate);
		user.setGender(gender);
		user.setPwd(pwd);
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		IEntity entity = commonService.findFirstByDescription(UserTable.class, "admin");
		user.setUser((UserTable)entity);
		entity = commonService.findFirstByDescription(Flag.class, "ACTIVE");
		user.setFlag((Flag)entity);
		commonService.add(user);
		return "saveRegistration";
		
	}
	
	/**
	 * @return the description
	 */	
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	@ModelAttribute("description")
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the firstname
	 */	
	public String getFirstName() {
		return firstname;
	}

	/**
	 * @param firstname the firstname to set
	 */
	@ModelAttribute("firstname")
	public void setFirstName(String firstname) {
		this.firstname = firstname;
	}


	/**
	 * @return the lastname
	 */	
	public String getLastName() {
		return lastname;
	}

	/**
	 * @param lastname the lastname to set
	 */
	@ModelAttribute("lastname")
	public void setLastName(String lastname) {
		this.lastname = lastname;
	}
	
	
	/**
	 * @return the emailid
	 */	
	public String getEmailId() {
		return emailid;
	}

	/**
	 * @param emailid the emailid to set
	 */
	@ModelAttribute("emailid")
	public void setEmailId(String emailid) {
		this.emailid = emailid;
	}
	
	
	/**
	 * @return the birthdate
	 */	
	public Date getBirthDate() {
		return birthdate;
	}

	/**
	 * @param birthdate the birthdate to set
	 * @throws DataAccessException 
	 */
	@ModelAttribute("birthdate")
	public void setBirthDate(String birthdate) throws DataAccessException {
		this.birthdate = ApplicationUtils.convertStringToDate(birthdate);
		
	}
	
	/**
	 * @return the gender
	 */	
	public String getGender() {
		return gender;
	}

	/**
	 * @param gender the gender to set
	 */
	@ModelAttribute("gender")
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	/**
	 * @return the pwd
	 */	
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd the pwd to set
	 */
	@ModelAttribute("pwd")
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}
